/*
  desc: 底部购物车
 */
<template>
  <div class="shopcart">
    <div class="content">
      <!-- 左边的购物车图标 -->
      <div class="content-left" @click="toggleList">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight':totalCount>0}">
            <span class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></span>
          </div>
          <div class="num" v-if="totalCount>0">{{totalCount}}</div>
        </div>
        <div class="price" :class="{'highlight':totalCount>0}">¥{{totalPrice}}</div>
        <div class="desc">另需配送费¥{{deliveryPrice}}元</div>
      </div>
      <!-- 右侧的结算 -->
      <div class="content-right" @click="pay">
        <div class="pay" :class="payClass">{{payDesc}}</div>
      </div>
    </div>
    <!-- 购物车清单 -->
    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @before-leave="beforeLeave"
      @leave="leave"
    >
      <div class="shopcart-list" v-show="listShow">
        <div class="list-header">
          <h1 class="title">购物车</h1>
          <span class="empty" @click="empty()">清空</span>
        </div>
        <div class="list-content" ref="menuWrapper">
          <ul>
            <li class="food" v-for="(food,index) in selectFoods" :key="index">
              <span class="name">{{food.name}}</span>
              <div class="price">
                ￥
                <span>{{food.price*food.count}}</span>
              </div>
              <div class="cartcontrol-wrapper">
                <cartcontrol :food="food"></cartcontrol>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </transition>
    <!-- 遮罩层 -->
    <div class="mask" v-if="this.listShow" @click="hideList"></div>
    <!-- 结算提示 -->
    <transition name="tips">
      <div class="tips" v-if="notEnoughtipsShow">
        <div class="tip-content">你个穷逼才点几个菜？还想让老娘送？</div>
      </div>
      <div class="tips" v-if="tipsShow">
        <div class="tip-content">你个穷逼配点外卖吗？洗洗睡吧！</div>
      </div>
    </transition>
  </div>
</template>
<script>
import cartcontrol from "../cartcontrol/cartcontrol";
import BScroll from "better-scroll"; // 引入betterscroll

export default {
  name: "",
  props: {
    // 选择的商品
    selectFoods: {
      type: Array,
      default() {
        return [
          {
            price: 10,
            count: 1
          }
        ];
      }
    },
    // 配送费
    deliveryPrice: {
      type: Number,
      default: 0
    },
    // 起送价
    minPrice: {
      type: Number,
      default: 0
    }
  },
  components: {
    cartcontrol
  },
  computed: {
    // 总价格
    totalPrice() {
      let total = 0;
      this.selectFoods.forEach(food => {
        console.log(food);
        total += food.price * food.count;
      });
      return total;
    },
    // 选中的数量
    totalCount() {
      let count = 0;
      this.selectFoods.forEach(food => {
        count += food.count;
      });
      return count;
    },
    // 结算
    payDesc() {
      if (this.totalPrice === 0) {
        return `¥${this.minPrice}起送`;
      } else if (this.totalPrice < this.minPrice) {
        let diff = this.minPrice - this.totalPrice;
        return `还差¥${diff}元起送`;
      } else {
        return `去结算`;
      }
    },
    // 结算的类名样式
    payClass() {
      if (this.totalPrice < this.minPrice) {
        return "not-enough";
      } else {
        return "enough";
      }
    },
    // 购物车是否折叠
    listShow() {
      if (!this.totalCount) {
        this.fold = true;
        return false;
      }
      let show = !this.fold;
      return show;
    }
  },
  data() {
    return {
      fold: true, // 购物车详情折叠
      tipsShow: false, // 结算提示
      notEnoughtipsShow: false // 未到起送价提示
    };
  },
  methods: {
    toggleList() {
      if (!this.totalCount) {
        return;
      }
      this.fold = !this.fold; // 购物车详情折叠
    },
    beforeEnter(el) {
      // 设置动画的起始状态
      el.style.transform = "translateY(0)";
      el.style.transition = "all 0.5s ease";
    },
    enter(el, done) {
      // 动画指定的过程
      el.offsetHeight;
      // 设置动画要达到的目标状态,Y轴方向高度是自己的高度
      el.style.transform = "translateY(-100%)";
      done();
    },
    // 出场动画
    beforeLeave(el) {
      el.style.transform = "translateY(-100%)";
    },
    leave(el) {
      el.style.transform = "translateY(0)";
    },
    // 初始化购物车列表滚动条
    _initScroll() {
      this.menuScroll = new BScroll(this.$refs.menuWrapper, {
        click: true,
        probeType: 3,
        mouseWheel: true
      });
    },
    // 清空购物车
    empty() {
      console.log(this.selectFoods);
      this.selectFoods.forEach(food => {
        food.count = 0;
      });
    },
    // 遮罩层
    hideList() {
      // this.listShow = false;
      this.fold = !this.fold;
    },
    // 结算
    pay() {
      if (this.totalPrice < this.minPrice) {
        this.notEnoughtipsShow = !this.notEnoughtipsShow;
      } else {
        this.tipsShow = !this.tipsShow;
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // dom渲染后初始化滚动条
      this._initScroll();
    });
  }
};
</script>
<style lang='stylus' scoped>
.shopcart {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 50;
  width: 100%;
  height: 46px;

  .content {
    display: flex;
    background: #141d27;
    font-size: 0;
    color: rgba(255, 255, 255, 0.4);
    position: relative;
    z-index: 10;

    .content-left {
      display: flex;
      flex: 1;
      height: 46px;

      .logo-wrapper {
        display: inline-block;
        position: relative;
        top: -10px;
        left: 0;
        margin: 0 10px;
        padding: 6px;
        width: 56px;
        height: 56px;
        box-sizing: border-box;
        vertical-align: top;
        border-radius: 50%;
        background: #141d27;

        .logo {
          width: 100%;
          height: 100%;
          text-align: center;
          border-radius: 50%;
          background: #2b343c;

          &.highlight {
            background-color: rgb(0, 160, 220);
          }

          .icon-shopping_cart {
            line-height: 44px;
            font-size: 24px;
            color: #80858a;

            &.highlight {
              color: #fff;
            }
          }
        }

        .num {
          position: absolute;
          top: 0;
          right: 0;
          width: 24px;
          height: 16px;
          line-height: 16px;
          text-align: center;
          border-radius: 16px;
          font-size: 12px;
          font-weight: 700;
          color: #fff;
          background: rgb(240, 20, 20);
          box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.4);
        }
      }

      .price {
        display: inline-block;
        margin-top: 12px;
        padding-right: 10px;
        vertical-align: top;
        height: 24px;
        line-height: 24px;
        font-size: 12px;
        font-weight: 700;
        border-right: 1px solid rgba(255, 255, 255, 0.1);

        &.highlight {
          color: #fff;
        }

        span {
          font-size: 16px;
        }
      }

      .desc {
        display: inline-block;
        margin-top: 12px;
        vertical-align: top;
        margin-left: 10px;
        line-height: 24px;
        font-size: 12px;
      }
    }

    // 右侧结算
    .content-right {
      flex: 0 0 90px;
      width: 90px;
      font-size: 12px;

      .pay {
        font-weight: 700;
        line-height: 46px;
        text-align: center;

        &.not-enough {
          background-color: #2b333b;
        }

        &.enough {
          background-color: #00b43c;
          color: #fff;
        }
      }
    }
  }

  // 购物车列表
  .shopcart-list {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #fff;
    z-index: 9;

    // 列表头部
    .list-header {
      height: 40px;
      line-height: 40px;
      padding: 0 18px;
      background-color: #ccc;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);

      .title {
        float: left;
        font-size: 14px;
        color: rgb(7, 17, 27);
      }

      .empty {
        float: right;
        font-size: 12px;
        color: rgb(0, 160, 220);
      }
    }

    .list-content {
      padding: 0 18px;
      max-height: 217px;
      // overflow-y scroll;
      overflow: hidden;
      background: #fff;

      .food {
        display: flex;
        position: relative;
        padding: 12 18px;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);

        // 商品名
        .name {
          font-size: 14px;
          color: rgb(7, 17, 27);
          line-height: 48px;
        }

        .price {
          position: absolute;
          right: 90px;
          bottom: 12px;
          line-height: 24px;
          font-size: 12px;
          color: rgb(240, 20, 20);

          span {
            font-size: 14px;
            font-weight: 700;
            color: rgb(240, 20, 20);
          }
        }

        // 增减按钮
        .cartcontrol-wrapper {
          position: absolute;
          right: 0;
          bottom: 12px;
        }
      }
    }
  }

  // .fold-enter-active, .fold-leave-active {
  // transition: all 1s;
  // transform: translate3d(0, -100%, 0);
  // }

  // .fold-enter, .fold-leave-to {
  // transform: translate3d(0, 0, 0);
  // }

  // 遮罩层
  .mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(7, 17, 27, 0.8);
    z-index: 8;
  }

  // 结算提示
  .tips {
    position: fixed;
    top: 40%;
    left: 0;
    width: 100%;
    text-align: center;

    .tip-content {
      width: 80%;
      margin: 0 auto;
      height: 48px;
      line-height: 48px;
      color: #fff;
      border: 1px solid rgba(7, 17, 27, 0.1);
      background-color: rgba(7, 17, 27, 0.8);
      border-radius: 10px;
    }
  }

  // 购物车清单动画
  .fade-enter-active, .fade-leave-active {
    opacity: 1;
    transition: opacity 0.8s ease;
  }

  .fade-enter, .fade-leave-to {
    opacity: 0;
  }

  // 结算提示动画
  .tips-enter-active, .tips-leave-active {
    opacity: 1;
    transition: all 1s ease;
  }

  .tips-enter, .tips-leave-to {
    opacity: 0;
  }
}
</style>
